﻿namespace BLL
{
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;
    using System.Web;
    using System.Linq;
    using System.Data.Entity;
    using System.Collections.Generic;
    using LBCMS.EntityFrameWork;
    using LBCMS.EntityFrameWork.FrameModel;

    public class Codes
    {
        public void Add(string CodeName)
        {
            using (BLLContext db = new BLLContext())
            {
                B_Code d = new B_Code();
                d.codeName = CodeName;
                d.sortNum = 0;
                db.B_Code.Add(d);
                db.SaveChanges();
            }

        }
 
        public bool IsExistCodeName(string CodeName)
        {
            using (BLLContext db = new BLLContext())
            {
                if (db.B_Code.Where(p => p.codeName == CodeName).ToList().Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }


        /// <summary>
        /// 排除当前CodeID，判断是否还存在相同的CodeName
        /// 编写日期：2011/4/8
        /// 编写人：zhouq
        /// </summary>
        public bool IsExistCodeName_ExceptCurrentCodeID(int CodeID, string CodeName)
        {
            using (BLLContext db = new BLLContext())
            {
                if (db.B_Code.Any(p => p.codeName == CodeName && p.id != CodeID))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }


        public void AddCodeItem(string ItemValue, string ItemText, int CodeID, int SortSQ)
        {
            using (BLLContext db
                = new BLLContext())
            {
                B_CodeValue d = new B_CodeValue();
                d.itemValue = ItemValue;
                d.itemText = ItemText;
                d.codeID = CodeID;
                d.sortNum = SortSQ;
                db.B_CodeValue.Add(d);
                db.SaveChanges();
            } 

        }

        public void Delete(int CodeID)
        {
            using (BLLContext db = new BLLContext())
            {
                B_Code d = new B_Code { id = CodeID };
                db.Entry(d).State = EntityState.Deleted;
                db.SaveChanges();

            }

        }

        public void DeleteCodeItem(int ID)
        {
            using (BLLContext db = new BLLContext())
            {
                B_CodeValue d = new B_CodeValue() { id = ID };
                db.Entry(d).State = EntityState.Deleted;
                db.SaveChanges();

            }
        }

        public B_Code Detail(int CodeID)
        {
            using (BLLContext db = new BLLContext())
            {
                return db.B_Code.First(p => p.id == CodeID);

            }
        }
 


        /// <summary>
        /// 修改为Linq
        /// 刘军 2017-5-15 09:17:05
        /// </summary>
        /// <param name="CodeName"></param>
        /// <param name="CodeText"></param>
        /// <returns></returns>
        public string GetCodeValueFromHash(string CodeName, string CodeText)
        {
             
            Dictionary<string, List<B_CodeValue>> allCodes = (Dictionary<string, List<B_CodeValue>>)System.Web.HttpContext.Current.Application["Codes"];
            List<B_CodeValue> values = allCodes[CodeName];
            var item = values.FirstOrDefault(p => p.itemText.Equals(CodeText));
            if (item != null)
            {
                return item.itemValue;
            }
            else
            {
                return "";
            }

        }


        /// <summary>
        /// 同上
        /// 刘军 2017-5-15 09:16:57
        /// </summary>
        /// <param name="CodeName"></param>
        /// <param name="CodeValue"></param>
        /// <returns></returns>
        public string GetCodeFromHash_CheckBox(string CodeName, string CodeValue)
        {
            if (CodeValue == null) { return ""; }
            string codeValues = "";
            string[] arrCode = CodeValue.Split(',');
            Dictionary<string, List<B_CodeValue>> allCodes = (Dictionary<string, List<B_CodeValue>>)System.Web.HttpContext.Current.Application["Codes"];
            List<B_CodeValue> values = allCodes[CodeName];
            foreach (var item in values)
            {
                if (arrCode.Contains(item.itemValue))
                {
                    codeValues += item.itemText + ";";
                }
                
            }

            return codeValues;
        }
 
        /// <summary>
        /// 根据代码名称获取所有代码子项
        /// 刘军 2017-5-12 14:21:38
        /// </summary>
        /// <param name="CodeName"></param>
        /// <returns></returns>
        public List<B_CodeValue> SelectCodeList(string CodeName)
        {
            Dictionary<string, List<B_CodeValue>> allCodes = (Dictionary<string, List<B_CodeValue>>)System.Web.HttpContext.Current.Application["Codes"];
            return allCodes[CodeName];
        }



          

    }
}

